import { h, ref } from 'vue';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import { renderOptionTag } from '@/utils';
import { useDictStore } from '@/store/modules/dict';

const dict = useDictStore();

export class State {
  public id = 0; // id
  public activityId = 0; // 活动id
  public uid = 0; // 用户id
  public number = ''; // 参赛号码
  public videoUrl = ''; // 视频路径
  public path = ''; // 预览地址
  public createAt = ''; // 添加时间
  public updateAt = ''; // 更新时间
  public status = 1; // 状态，0正常

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'id',
    component: 'NInputNumber',
    label: 'id',
    componentProps: {
      placeholder: '请输入id',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'status',
    component: 'NSelect',
    label: '状态，0正常',
    defaultValue: null,
    componentProps: {
      placeholder: '请选择状态，0正常',
      options: dict.getOption('sys_normal_disable'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'activityTitle',
    component: 'NInput',
    label: '活动名称',
    componentProps: {
      placeholder: '请输入活动名称',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'activityUserName',
    component: 'NInput',
    label: '姓名',
    componentProps: {
      placeholder: '请输入姓名',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'activityUserPhone',
    component: 'NInput',
    label: '手机号',
    componentProps: {
      placeholder: '请输入手机号',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: 'id',
    key: 'id',
    align: 'left',
    width: -1,
  },
  {
    title: '活动id',
    key: 'activityId',
    align: 'left',
    width: -1,
  },
  {
    title: '用户id',
    key: 'uid',
    align: 'left',
    width: -1,
  },
  {
    title: '参赛号码',
    key: 'number',
    align: 'left',
    width: -1,
  },
  {
    title: '视频路径',
    key: 'videoUrl',
    align: 'left',
    width: -1,
  },
  {
    title: '预览地址',
    key: 'path',
    align: 'left',
    width: -1,
  },
  {
    title: '添加时间',
    key: 'createAt',
    align: 'left',
    width: -1,
  },
  {
    title: '更新时间',
    key: 'updateAt',
    align: 'left',
    width: -1,
  },
  {
    title: '状态，0正常',
    key: 'status',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('sys_normal_disable', row.status);
    },
  },
  {
    title: '活动名称',
    key: 'activityTitle',
    align: 'left',
    width: -1,
  },
  {
    title: '姓名',
    key: 'activityUserName',
    align: 'left',
    width: -1,
  },
  {
    title: '手机号',
    key: 'activityUserPhone',
    align: 'left',
    width: -1,
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['sys_normal_disable']);
}